package leetcode.string;

import java.util.*;

/**
 * 在老式手机上，用户通过数字键盘输入，手机将提供与这些数字相匹配的单词列表。每个数字映射到0至4个字母。给定一个数字序列，实现一个算法来返回匹配单词的列表。你会得到一张含有有效单词的列表。映射如下图所示：
 * 示例 1:
 *
 * 输入: num = "8733", words = ["tree", "used"]
 * 输出: ["tree", "used"]
 * 示例 2:
 *
 * 输入: num = "2", words = ["a", "b", "c", "d"]
 * 输出: ["a", "b", "c"]
 */
public class T9 {

    char[] cArray = {'2','2','2','3','3','3','4','4','4','5','5','5','6','7','7','7','7','8','8','8','9','9','9','9'};

    private static Map<Character,String> characterStringMap = new HashMap<>();

    private static void init(){
        characterStringMap.put('a',"2");
        characterStringMap.put('b',"2");
        characterStringMap.put('c',"2");
        characterStringMap.put('d',"3");
        characterStringMap.put('e',"3");
        characterStringMap.put('f',"3");
        characterStringMap.put('g',"4");
        characterStringMap.put('h',"4");
        characterStringMap.put('i',"4");
        characterStringMap.put('j',"5");
        characterStringMap.put('k',"5");
        characterStringMap.put('l',"5");
        characterStringMap.put('m',"6");
        characterStringMap.put('n',"6");
        characterStringMap.put('o',"6");
        characterStringMap.put('p',"7");
        characterStringMap.put('q',"7");
        characterStringMap.put('r',"7");
        characterStringMap.put('s',"7");
        characterStringMap.put('t',"8");
        characterStringMap.put('u',"8");
        characterStringMap.put('v',"8");
        characterStringMap.put('w',"9");
        characterStringMap.put('x',"9");
        characterStringMap.put('y',"9");
        characterStringMap.put('z',"9");
    }

    public static List<String> getValidT9Words(String num, String[] words) {
        init();
        List<String> strings = new ArrayList<>(words.length);
        StringBuilder stringBuilder;
        for (String word : words) {
            stringBuilder = new StringBuilder();
            char[] wordC = word.toCharArray();
            for (char c : wordC) {
                stringBuilder.append(characterStringMap.get(c));
            }
            if (stringBuilder.toString().equals(num)){
                strings.add(word);
            }
        }
        return strings;
    }

}
